﻿using System;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using Object = UnityEngine.Object;


public class TooL
{
    private static TooL instance;

    public static TooL GetInstance
    {
        get
        {
            if (instance == null) instance = new TooL();
            return instance;
        }
    }

    public enum Color
    {
        None = 0,
        GRAY,
        WHITE,
        BLUE,
        PURPLE,
        Yellow,
        Max
    }

    public enum TipsPageType
    {
        haveCancelPage,
        noCancelPage,
    }

    // 加载tips对话框
    public static void ShowTipsPage(string content, TipsPageType pageType, TipsDialogCallBack confirmCB,
        TipsDialogCallBack cancelCB = null,
        string confirmBtnStr = "确定", string cancelBtnStr = "取消")
    {
        var root = GameObject.Find("SpecialCanvas/specialAddNode");
        if (root)
        {
            if (pageType == TipsPageType.haveCancelPage)
            {
                LoadTipsHaveCancelPage(root, content, confirmCB, cancelCB, confirmBtnStr, cancelBtnStr);
            }
            else if (pageType == TipsPageType.noCancelPage)
            {
                LoadTipsNoCancelPage(root, content, confirmCB, confirmBtnStr);
            }
        }
        else
        {
            Debug.Log("[Tool] 没有发现specialAddNode节点 ...");
        }
    }

    private static void LoadTipsHaveCancelPage(GameObject root, string content, TipsDialogCallBack confirmCB,
        TipsDialogCallBack cancelCB, string confirmBtnStr, string cancelBtnStr)
    {
        var tips = LoadPrefab(root, "TipsHaveCancelPage");
        var script = tips.GetComponent<TipsDialogPage>();
        if (script)
        {
            script.SetContent(content);
            script.SetConfirmCallBack(confirmCB);
            if (cancelCB != null)
                script.SetCancleCallBack(cancelCB);
            script.SetConfirmBtnContent(confirmBtnStr);
            script.SetCancelBtnContent(cancelBtnStr);
        }


    }

    private static void LoadTipsNoCancelPage(GameObject root, string content, TipsDialogCallBack confirmCB,
        string confirmBtnStr)
    {
        var tips = LoadPrefab(root, "TipsNoCancelPage");
        var script = tips.GetComponent<TipsDialogPage>();
        if (script)
        {
            script.SetContent(content);
            script.SetConfirmCallBack(confirmCB);
            script.SetConfirmBtnContent(confirmBtnStr);
        }
    }

    //加载Notice提示框
    public static void ShowNoticePage(string content)
    {
        var root = GameObject.Find("SpecialCanvas/specialAddNode");
        if (root)
        {
            LoadNoticePage(root, content);
        }
        else
        {
            Debug.Log("[Tool] 没有发现specialAddNode节点 ...");
        }
    }

    private static void LoadNoticePage(GameObject root, string content)
    {
        var temp = LoadPrefab(root, "NoticePage");
        temp.GetComponent<NoticePage>().SetContent(content);
    }

    //加载预设体
    public static GameObject LoadPrefab(GameObject parentNode, string prefabUrl)
    {
        Debug.Log("加载预设体：" + prefabUrl);
        var clone = Object.Instantiate(Resources.Load("SHPrefab/" + prefabUrl) as GameObject);
        clone.transform.SetParent(parentNode.transform, false);
        clone.name = prefabUrl;
        clone.transform.localPosition = new Vector3(0, 0, 0);
        clone.transform.localRotation = parentNode.transform.rotation;
        clone.transform.localScale = Vector3.one;
        return clone;
    }

    //生成手牌动画
    public static GameObject DealHandCardAni(GameObject parentNode, string prefabUrl)
    {
        var temp = LoadPrefab(parentNode, prefabUrl);
        //temp.transform.SetParent(parentNode.transform, true);
        //temp.transform.localScale = Vector3.one;

        //var tweener = temp.transform.DOMove(parentNode.transform.position, 0.3f);
        //tweener.OnComplete(() => temp.GetComponent<LayoutElement>().ignoreLayout = false);
        //GameRoomPage.GetInstance().SetAudAndPlay(3);       // 发牌音效
        return temp;
    }

    //删除所有子物体
    public static void DestroyAllChildren(GameObject node)
    {
        if (node != null)
            for (var i = 0; i < node.transform.childCount; i++)
                Object.Destroy(node.transform.GetChild(i).gameObject);
    }

    //检查并删除面板
    public static void CheckAndDeleteGameObject(string objName)
    {
        GameObject obj = GameObject.Find(objName);
        if (obj)
        {
            Object.Destroy(obj);
        }
    }

    public static string strGetNewline(string str)
    {
        return str.Replace(@"\\n", "\n");
    }

    public static string GetImagePath(string imageName, int imageType)
    {
        var imagePath = "";
        switch (imageType)
        {
            case 0:
                imagePath = string.Format("MJSprite/{0}", imageName);
                return imagePath;
            case 1:
                imagePath = string.Format("ZJHSprite/{0}", imageName);
                return imagePath;
            case 2:
                imagePath = string.Format("PDKSprite/{0}", imageName);
                return imagePath;
            default:
                throw new Exception("Image 类型出错");
        }
    }

    /// <summary>
    ///     文字颜色
    /// </summary>
    /// <param name="color"></param>
    /// <returns></returns>
    public static string NameWithColor(Color color)
    {
        switch (color)
        {
            case Color.GRAY:
                return "[999999]{0}[-]";
            case Color.WHITE:
                return "[ffffff]{0}[-]";
            case Color.BLUE:
                return "[00ccff]{0}[-]";
            case Color.PURPLE:
                return "[cc00cc]{0}[-]";
            case Color.Yellow:
                return "[ffff00]{0}[-]";
            default:
                throw new Exception("文字颜色出错");
        }
    }

    public static void ClearMemory()
    {
        Resources.UnloadUnusedAssets();
        GC.Collect();
    }
    /// <summary>
    /// 小于25个字
    /// </summary>
    /// <param name="TiSI"></param>
    public static void WenXinTiSi(string TiSI)
    {
        GameObject clone = Object.Instantiate(Resources.Load("Prefab/TiSi/WinXinTiSi") as GameObject);
        clone.transform.SetParent(GameObject.Find("addNode").transform);
        clone.transform.localPosition = new Vector3(0, 0, 0);
        clone.transform.localScale = Vector3.one;
        clone.transform.GetChild(0).GetChild(0).GetComponent<Text>().text = TiSI;
    }

}